<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
class Tree{
	private $rawList = array();
	private $formatList = array();
	private $icon = array('│','├','└');
	private $field = array();
	public function __construct($field=array()){
		$this->field['id'] = isset($field['0']) ? $field['0'] : 'id';
		$this->field['pid'] = isset($field['1']) ? $field['1'] : 'pid';
 		$this->field['title'] = isset($field['2']) ? $field['2'] : 'title';
 		$this->field['fulltitle'] = isset($field['3']) ? $field['3'] : 'fulltitle';
	}
	public function getChild($pid,$data=array()){
		$childs = array();
		if(empty($data))	$data=$this->rawList;
		foreach($data as $Category){
			if($Category[$this->field['pid']]==$pid)	$childs[] = $Category;
		}
		return $childs;
	}
	public function getTree($data,$id=0){
		if(empty($data))	return false;
		$this->rawList = array();
		$this->formatList = array();
		$this->rawList = $data;
		$this->_searchList($id);
		return $this->formatList;
	}
	private function _searchList($id=0,$space=""){
		$childs = $this->getChild($id);
		if(!$n = count($childs))	return;
		$cnt = 1;
		for($i=0;$i<$n;$i++){
			$pre= "";
			$pad= "";
			if($n == $cnt){
				$pre = $this->icon[2];
			}else{
				$pre = $this->icon[1];
				$pad = $space ? $this->icon[0] : "";
			}
			$childs[$i][$this->field['fulltitle']] = ($space ? $space.$pre : "").$childs[$i][$this->field['title']];
			$this->formatList[] = $childs[$i];
			$this->_searchList($childs[$i][$this->field['id']],$space.$pad."&nbsp;&nbsp;");
			$cnt++;
		}
	}
}
